const winston = require('winston');
const { formatDateTime } = require('./dateFormatter');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.printf(info => {
      const formattedTime = formatDateTime(info.timestamp);
      const metadata = info.metadata ? ` ${JSON.stringify(info.metadata)}` : '';
      const divider = '-'.repeat(80); // 创建分割线
      return `${divider}\n${formattedTime} ${info.level}: ${info.message}${metadata}`;
    })
  ),
  transports: [
    new winston.transports.File({ 
      filename: 'logs/error.log', 
      level: 'error',
      maxsize: 5242880, // 5MB
      maxFiles: 5
    }),
    new winston.transports.File({ 
      filename: 'logs/combined.log',
      maxsize: 5242880, // 5MB
      maxFiles: 5
    })
  ]
});

if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.combine(
      winston.format.timestamp(),
      winston.format.printf(info => {
        const formattedTime = formatDateTime(info.timestamp);
        return `${formattedTime} ${info.level}: ${info.message}`;
      })
    )
  }));
}

module.exports = logger; 